package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.TravelComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.repository.TravelCommentMongoRepository;
import cn.wolfcode.wolf2w.service.ITravelCommentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TravelCommentServiceImpl implements ITravelCommentService {

    @Autowired
    private TravelCommentMongoRepository repository;
    @Autowired
    private MongoTemplate template;


    @Override
    public void save(TravelComment travelComment) {
        travelComment.setId(null);
        repository.save(travelComment);
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public void update(TravelComment travelComment) {
        repository.save(travelComment);
    }

    @Override
    public TravelComment find(String id) {
        TravelComment travelComment = repository.findById(id).orElse(null);

        return travelComment;
    }

    @Override
    public List<TravelComment> findAll() {
        List<TravelComment> list = repository.findAll();
        return list;
    }

    @Override
    public void commentAdd(TravelComment travelComment, UserInfo userInfo) {
        BeanUtils.copyProperties(userInfo,travelComment);
        travelComment.setUserId(userInfo.getId());
        travelComment.setCreateTime(new Date());
        if (travelComment.getType() == 1){
            travelComment.setRefComment(this.find(travelComment.getRefComment().getId()));
        }
        this.save(travelComment);
    }

    @Override
    public List<TravelComment> queryCommentsByTravelId(String travelId) {
        List<TravelComment> list = repository.findByTravelId(travelId);
        return list;
    }


}
